<?php

namespace backend\controllers;

use Yii;
use common\models\LogoAdv;
use backend\models\LogoAdvSearch;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * LogoAdvController implements the CRUD actions for LogoAdv model.
 */
class LogoAdvController extends MyController
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all LogoAdv models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new LogoAdvSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single LogoAdv model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new LogoAdv model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new LogoAdv();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['index', 'id' => $model->logo_adv_id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing LogoAdv model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['index', 'id' => $model->logo_adv_id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing LogoAdv model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the LogoAdv model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return LogoAdv the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = LogoAdv::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    
    public function actionRefreshCache()
    {
        $datetime = date('Y-m-d 00:00:00');
        $connection = Yii::$app->db;
        
        $sql = "SELECT * FROM d_try.`logo_adv`  where `status`=1 and '$datetime' between recommend_starttime and recommend_endtime ORDER BY add_time desc limit 1";     
        $result = $connection->createCommand($sql)->queryAll();

        if(!empty($result))
        {
            $index_manage = array();
            foreach($result as $v)
            {
                $index_manage[] = $v;
            }
            \Yii::$app->redis_index->set('logo_adv',  serialize($index_manage));
            \Yii::$app->redis_old->set('logo_adv',  serialize($index_manage));
        }else
        {
            \Yii::$app->redis_index->set('logo_adv',  '');
            \Yii::$app->redis_old->set('logo_adv',  '');
        }
        $json_array = ['flag' => 1];
        return json_encode($json_array);
    }
    
}
